import {expect} from 'chai';
import {GreyMarketExploitation} from '../../../src/server/cards/underworld/GreyMarketExploitation';
import {testGame} from '../../TestGame';
import {cast, runAllActions} from '../../TestingUtils';
import {OrOptions} from '../../../src/server/inputs/OrOptions';
import {SelectOption} from '../../../src/server/inputs/SelectOption';
import {SelectResource} from '../../../src/server/inputs/SelectResource';
import {Units} from '../../../src/common/Units';

describe('GreyMarketExploitation', () => {
  it('Can play', () => {
    const card = new GreyMarketExploitation();
    const [/* game */, player] = testGame(2);

    player.underworldData.corruption = 1;

    expect(card.canPlay(player)).is.false;

    player.underworldData.corruption = 2;

    expect(card.canPlay(player)).is.true;
  });

  it('Can act', () => {
    const card = new GreyMarketExploitation();
    const [/* game */, player] = testGame(2);
    player.cardsInHand.push(card);

    expect(card.canAct(player)).is.false;

    player.underworldData.corruption = 1;

    expect(card.canAct(player)).is.true;

    player.underworldData.corruption = 0;
    player.megaCredits = 1;

    expect(card.canAct(player)).is.true;
  });

  it('action', () => {
    const card = new GreyMarketExploitation();
    const [game, player] = testGame(2);

    player.underworldData.corruption = 1;
    player.megaCredits = 1;

    cast(card.action(player), undefined);
    runAllActions(game);

    const orOptions = cast(player.popWaitingFor(), OrOptions);
    const mc = cast(orOptions.options[0], SelectOption);
    const corruption = cast(orOptions.options[1], SelectOption);

    mc.cb(undefined);
    runAllActions(game);
    const select1Resource = cast(player.popWaitingFor(), SelectResource);
    select1Resource.cb('plants');
    expect(player.stock.asUnits()).deep.eq(Units.of({plants: 1}));
    expect(player.megaCredits).eq(0);

    corruption.cb(undefined);
    runAllActions(game);
    const select3Resources = cast(player.popWaitingFor(), SelectResource);
    select3Resources.cb('steel');
    expect(player.stock.asUnits()).deep.eq(Units.of({plants: 1, steel: 3}));
    expect(player.underworldData.corruption).eq(0);
  });
});
